package com.yunduansing.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.commons.DbCommon.Grid;
import com.yunduansing.core.dao.TestDao;
import com.yunduansing.core.model.Test;
import com.yunduansing.core.service.TestService;
import com.yunduansing.core.utils.CommonUtil;
import com.yunduansing.core.utils.ResultUtil;

@Service
public class TestServiceImpl implements TestService {
	@Autowired
	private TestDao testDao;
	private Logger log=(Logger) LoggerFactory.getLogger(TestServiceImpl.class);

	public Test getById(int id) {
		return testDao.getById(id);
	}

	public List<Test> getList() {
		return testDao.getList();
	}

	public ResultUtil save(Test m) {
		log.info(m.getId()>0?"修改：":"新增"+CommonUtil.toJsonWithGson(m));
		if(m.getId()>0)
			return ResultUtil.init(testDao.update(m));
		return ResultUtil.init(testDao.add(m));
	}

	public ResultUtil delete(List<Integer> ids) {
		if(ids.size()<=0)
			return ResultUtil.init(false, "请选择要删除的数据");
		return ResultUtil.init(testDao.delete(ids));
	}

	public Object pagination(Grid grid) {
		DbCommon.Pager pager=new DbCommon.Pager();
		pager.setIndex(grid.getIndex());
		grid.setIndex((grid.getIndex()-1)*grid.getSize());
		pager.setData(testDao.pagination(grid));
		pager.setRows(testDao.pageCount());
		return pager;
	}
	
	@Transactional
	public void testTrans(int count){
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(2);
		ids.add(3);
		testDao.delete(ids);
		if(count>0)
			System.out.println("事务控制成功");
		else{
			System.out.println("事务提交失败");
			throw new RuntimeSqlException("事务提交失败");
		}
	}

}
